package com.cyz.user.userService.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyz.common.bean.Buttongroup;
import com.cyz.common.enums.MyEnums;
import com.cyz.common.mapper.ButtonMapper;
import com.cyz.common.mapper.ButtongroupMapper;
import com.cyz.user.userService.ButtongroupService;
import com.cyz.common.utils.BeanCopyUtil;
import com.cyz.common.utils.R;
import com.cyz.user.vo.ButtongroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户端免费按钮分类接口实现类
 */
@Service
public class ButtongroupServiceImpl extends ServiceImpl<ButtongroupMapper, Buttongroup>
    implements ButtongroupService {

    @Autowired
    private ButtonMapper buttonMapper;

    @Override
    public R queryFreeButtonGroups() {
        //查询数据
        List<Buttongroup> list = list();
        //vo转化
        List<ButtongroupVo> buttongroupVos = BeanCopyUtil.copyBeanList(list, ButtongroupVo.class);
        //错误判断
        if(buttongroupVos.isEmpty()){
            return R.error(MyEnums.QUERY_FAIL);
        }
        return R.success(buttongroupVos);
    }

    @Override
    public R queryFreeButtonGroupsAndButtons() {
        //查询数据
        List<Buttongroup> list = list();
        //vo转化
        List<ButtongroupVo> buttongroupVos = BeanCopyUtil.copyBeanList(list, ButtongroupVo.class);
        //查询各分组里面的按钮
        buttongroupVos
                .forEach(item -> {
                            item.setButtons(buttonMapper.getButtonsByGroupId((item.getId())));
                            item.setSelected(0);
                });
        //错误判断
        if(buttongroupVos.isEmpty()){
            return R.error(MyEnums.QUERY_FAIL);
        }
        return R.success(buttongroupVos);
    }
}




